package com.platform.mobile.operator;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.platform.base.SysException;
import com.platform.mobile.bean.AddPaitentRequest;
import com.platform.mobile.bean.ReqBean;
import com.platform.mobile.bean.ResBean;
import com.platform.mobile.bean.ResultBean;
import com.platform.mobile.factory.Operator;
import com.platform.mobile.service.PaitentService;

/**
 * 新增病人资料信息
 * @author inven
 *
 */
@Component
public class AddPaitent extends Operator{

	private static Logger log = LoggerFactory.getLogger(AddPaitent.class);
	
	@Autowired
	private PaitentService service;
	
	@Override
	public ResBean operate(ReqBean reqBean) throws SysException {
		
		log.info("进入到AddPaitent业务处理类，处理[" + reqBean.getClientInfo() + "]用户请求");
		ResBean resBean = new ResBean();
		resBean.setClientInfo(reqBean.getClientInfo());
		resBean.setJsonrpc("2.0");
		resBean.setMethod("AddPaitent");
		ResultBean result = new ResultBean();
		
		AddPaitentRequest request = new AddPaitentRequest();
		
		try{
			
			Object params = reqBean.getParams();
			if(params != null){
				
				@SuppressWarnings("unchecked")
				LinkedHashMap<String, Object> paramMap = (LinkedHashMap<String, Object>)params;
				request.setName(paramMap.get("name").toString());
				request.setSex(paramMap.get("sex").toString());
				request.setPhone(paramMap.get("phone").toString());
				
				@SuppressWarnings("deprecation")
				Date birthday = new Date(paramMap.get("birthday").toString());
/*				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
				birthday = format.parse(paramMap.get("birthday").toString());
*/				
				request.setBirthday(birthday);
				request.setMarriage((int)paramMap.get("marriage"));
				request.setAddress(paramMap.get("address").toString());
				
				service.addPaitent(request);
				
				result.setResultStatus("1");
				result.setInfo("业务处理成功");
				result.setData(null);
			}
			
		}catch(Exception e){
			e.printStackTrace();
			result.setResultStatus("0");
			result.setInfo("服务端异常");
			result.setData(null);
		}
		
		resBean.setResult(result);
		return resBean;
	}

}
